﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;

namespace Little.Soldier.SWIFT
{
    [Serializable]
    public abstract class SwiftTagListBlock : SwiftBlockBase
    {
        private List<SwiftTag> _Tags = new List<SwiftTag>();

        public virtual bool ContainsBlockData
        {
            get
            {
                return false;
            }
        }

        public virtual int TagCount
        {
            get
            {
                if (this._Tags == null)
                    return 0;
                else
                    return this._Tags.Count;
            }
        }

        internal virtual Hashtable TagMap
        {
            get
            {
                if (this._Tags == null)
                    return (Hashtable) null;
                Hashtable hashtable = new Hashtable();
                foreach (SwiftTag swiftTag in this.Tags)
                {
                    if (!hashtable.ContainsKey((object) swiftTag.Name))
                        hashtable.Add((object) swiftTag.Name, (object) swiftTag.Value);
                }
                return hashtable;
            }
        }

        public virtual List<SwiftTag> Tags
        {
            get
            {
                return this._Tags;
            }
            set
            {
                if (value == null)
                    return;
                this._Tags = value;
            }
        }

        public virtual bool IsEmpty
        {
            get
            {
                if (this._Tags == null)
                    return true;
                else
                    return this._Tags.Count == 0;
            }
        }

        public virtual int Size
        {
            get
            {
                if (this._Tags == null)
                    return 0;
                else
                    return this._Tags.Count;
            }
        }

        public SwiftTagListBlock(SwiftMessage swiftMessage)
          : base(swiftMessage)
        {
        }

        public virtual List<SwiftTag[]> GetTagSequences()
        {
            List<string> list1 = new List<string>();
            List<SwiftTag[]> list2 = new List<SwiftTag[]>();
            List<SwiftTag> list3 = new List<SwiftTag>();
            foreach (SwiftTag swiftTag in this.Tags)
            {
            if (list1.Contains(swiftTag.Name))
            {
                list1.Clear();
                list2.Add(list3.ToArray());
                list3 = new List<SwiftTag>();
            }
            list1.Add(swiftTag.Name);
            list3.Add(swiftTag);
            }
            list2.Add(list3.ToArray());
            return list2;
        }

        public virtual void AddTag(SwiftTag swiftTag)
        {
            if (this._Tags == null)
                goto label_2;
        label_1:
            this._Tags.Add(swiftTag);
            return;
        label_2:
            this._Tags = new List<SwiftTag>();
            goto label_1;
        }

        public virtual void AddTags(SwiftTag[] swiftTags)
        {
            foreach (SwiftTag swiftTag in swiftTags)
            this.Tags.Add(swiftTag);
        }

        public virtual bool ContainsTag(string tagName)
        {
            foreach (SwiftTag swiftTag in this.Tags)
            {
            if (swiftTag != null && swiftTag.Name != null && swiftTag.Name == tagName)
                return true;
            }
            return false;
        }

        public override bool Equals(object obj)
        {
            if (this != obj)
            goto label_8;
        label_7:
            return true;
        label_8:
            if (!base.Equals(obj) || this.GetType() != obj.GetType())
            return false;
            SwiftTagListBlock swiftTagListBlock = (SwiftTagListBlock) obj;
            if (this._Tags == null)
            {
                if (swiftTagListBlock._Tags != null)
                    return false;
                else
                    goto label_7;
            }
            else if (!this._Tags.Equals((object) swiftTagListBlock._Tags))
            return false;
            else
            goto label_7;
        }

        public override int GetHashCode()
        {
            return 31 * base.GetHashCode() + (this._Tags != null ? this._Tags.GetHashCode() : 0);
        }

        public virtual SwiftTag GetTag(int i)
        {
            return this._Tags[i];
        }

        public virtual SwiftTag GetTagByName(string key)
        {
            switch (key)
            {
            case null:
                return (SwiftTag) null;
            default:
                foreach (SwiftTag swiftTag in this.Tags)
                {
                    try
                    {
                        if (swiftTag.Name.ToUpper() == key.ToUpper())
                        return swiftTag;
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.ToString());
                    }
                }
                return (SwiftTag) null;
            }
        }

        public virtual SwiftTag GetTagByNumber(int key)
        {
            if (key < 0)
            return (SwiftTag) null;
            foreach (SwiftTag swiftTag in this.Tags)
            {
                try
                {
                    if (swiftTag.NameAsInt == key)
                        return swiftTag;
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.ToString());
                }
            }
            return (SwiftTag) null;
        }

        public virtual int GetTagCount(string key)
        {
            int num = 0;
            if (this._Tags != null)
            goto label_7;
        label_6:
            return num;
        label_7:
            using (List<SwiftTag>.Enumerator enumerator = this.Tags.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    if (enumerator.Current.Name == key)
                    ++num;
                }
                goto label_6;
            }
        }

        public virtual List<SwiftTag> GetTagsByName(string name)
        {
            List<SwiftTag> list = new List<SwiftTag>();
            foreach (SwiftTag swiftTag in this.Tags)
            {
            if (swiftTag.Name == null && name == null)
                list.Add(swiftTag);
            if (swiftTag.Name != null && name != null && swiftTag.Name == name)
                list.Add(swiftTag);
            }
            return list;
        }

        public virtual string GetTagValue(string key)
        {
            SwiftTag tagByName = this.GetTagByName(key);
            if (tagByName != null)
                return tagByName.Value;
            else
                return (string) null;
        }

        public virtual List<string> GetTagValues(string key)
        {
            List<string> list = new List<string>();
            if (this._Tags != null)
            {
                foreach (SwiftTag swiftTag in this.Tags)
                {
                    if (swiftTag.Name == key)
                    list.Add(swiftTag.Value);
                }
            }
            return list;
        }

        public virtual int RemoveAll(string name)
        {
            int num = 0;
            List<SwiftTag> tagsByName = this.GetTagsByName(name);
            for (int index = 0; index < tagsByName.Count; ++index)
            {
                this._Tags.Remove(tagsByName[index]);
                ++num;
            }
            return num;
        }

        public virtual void RemoveTagsByName(string tag)
        {
            if (this.Tags == null)
            return;
            for (int index = 0; index < this.Tags.Count; ++index)
            {
                if (this.Tags[index].Name != null && this.Tags[index].Name == tag)
                {
                    this.Tags.Remove(this.Tags[index]);
                    --index;
                }
            }
        }

        internal override XmlNode ToXml(XmlDocument doc)
        {
            XmlElement element1 = doc.CreateElement("Block");
            XmlAttribute attribute = doc.CreateAttribute("Name");
            attribute.Value = this.BlockName;
            element1.Attributes.Append(attribute);
            foreach (SwiftTag swiftTag in this.Tags)
            {
                string str = swiftTag.Name;
                if (!char.IsLetter(str, 0))
                    str = "Tag_" + str;
                XmlElement element2 = doc.CreateElement(str);
                element2.InnerText = swiftTag.Value;
                element1.AppendChild((XmlNode) element2);
            }
            return (XmlNode) element1;
        }
    }
}
